import sockettool from "../../common/js/sockettool.js"
import httptool from "../../common/js/httptool.js"
export default {
    data: {
        defaultTime: "",
        hour:12,
        minute:10,
        //time: "12:00",
        gg:10,
        setTimeoutok:null,
        lists:[

        ]
    },
    onInit() {
        this.defaultTime = this.now();
        httptool.http_post_callback(this.$app.$def.datas.serverurl+"/timerOfFeeding_get",{name: "test"},this.refresh)

    },
    refresh(data)
    {
        for (var index in data)
        {
            this.lists.push(data[index])
            console.log(this.lists[index].hour)
            this.setScheduledTask(this.lists[index].hour, this.lists[index].minute,this.lists[index].weight,this.doTask);
        }
    },
    handleChange(data) {
        //this.time = this.concat(data.hour, data.minute);
        this.hour = data.hour;
        this.minute = data.minute;
    },
    now() {
        const date = new Date();
        const hours = date.getHours();
        const minutes = date.getMinutes();
        return this.concat(hours, minutes);
    },
    changeText(e){
        console.log(JSON.stringify(e.target.attr.index));
        this.lists.splice(e.target.attr.index,1);
    },

    fill(value) {
        return (value > 9 ? "" : "0") + value;
    },

    concat(hours, minutes) {
        return `${this.fill(hours)}:${this.fill(minutes)}`;
    },
    setvalue(e){
        console.log("e.value ："+e.value+" "+this.gg);
        this.gg = e.value;
    },
    buclick(){
       let bufdata = {
        hour:this.hour,
        minute:this.minute,
        weight:this.gg
        }
        this.lists.push(bufdata);

        this.setScheduledTask(this.hour, this.minute,this.gg,this.doTask);
    },
    setScheduledTask(hour, minute,weight,callTask) {
        let taskTime = new Date();
        taskTime.setHours(hour);
        taskTime.setMinutes(minute);
        let timeDiff = taskTime.getTime() - (new Date()).getTime(); // 获取时间差
        timeDiff = timeDiff > 0 ? timeDiff : (timeDiff + 24 * 60 * 60 * 1000);
        setTimeout(()=> {
            callTask(weight) // 首次执行
            setInterval(callTask, 24 * 60 * 60 * 1000); // 24小时为循环周期
        }, timeDiff);
    },doTask(weight) {
        this.$emit('setautoweight',weight);
        sockettool.socketSend(this.$app.$def.datas.tcp,weight)
    }


}
